package com.incms.web.common.dao;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.google.common.collect.Maps;
import com.incms.web.common.util.Page;
/**
 * 项目名称：inCms    
 * 类名称：BaseDaoImpl    
 * 类描述： Basedao实现类   
 * 创建人：liujunqing    
 * 创建时间：2015年10月15日  
 * @version 1.0    
 *
 */
@Repository("baseDao")
public abstract class BaseDaoImpl implements BaseDao {
	
	@Resource(name = "sqlSession")
	public SqlSession sqlSession;

	@Override
	public Long save(String sqlKey, Object value) {
		return Long.valueOf(sqlSession.insert(sqlKey, value));
	}

	@Override
	public Long update(String sqlKey, Object value) {
		return Long.valueOf(sqlSession.update(sqlKey,value));
	}

	@Override
	public Long delete(String sqlKey, Object value) {
		return Long.valueOf(sqlSession.delete(sqlKey, value));
	}

	@Override
	public <T> T findOne(String sqlKey, Object value) {
		return sqlSession.selectOne(sqlKey, value);
	}

	@Override
	public <T> List<T> findList(String sqlkey, Object value) {
		return sqlSession.selectList(sqlkey, value);
	}

	@Override
	public <T> Page<T> queryForListPage(String sqlKey, Object params, Page<T> page) {
		Map<String,Object> map = Maps.newHashMap();
		map.put("e", params);
		map.put("page", page);
		map.put("countFalg", true);
		Integer count = (Integer) findOne(sqlKey + "Count", map);
		if( count == null || count ==0){
			page.setCount(0);
		}
		
		page.setCount(count);
		page.setList(this.findList(sqlKey, map));
		return page;
	}
	
	
	public <T> List<T> queryForListPageCount(String sqlKey,Object params,Page<T> page){
		Map<String,Object> map = Maps.newHashMap();
		map.put("e", params);
		map.put("countFalg", true);
		map.put("page", page);
		return findList(sqlKey, map);
	}

}
